package cn.ibizlab.businesscentral.core.rest;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.math.BigInteger;
import java.util.HashMap;
import lombok.extern.slf4j.Slf4j;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cglib.beans.BeanCopier;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.HttpStatus;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.util.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.validation.annotation.Validated;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import cn.ibizlab.businesscentral.core.dto.*;
import cn.ibizlab.businesscentral.core.mapping.*;
import cn.ibizlab.businesscentral.core.odoo_sale.domain.Sale_product_configurator;
import cn.ibizlab.businesscentral.core.odoo_sale.service.ISale_product_configuratorService;
import cn.ibizlab.businesscentral.core.odoo_sale.filter.Sale_product_configuratorSearchContext;
import cn.ibizlab.businesscentral.util.annotation.VersionCheck;

@Slf4j
@Api(tags = {"销售产品配置器" })
@RestController("Core-sale_product_configurator")
@RequestMapping("")
public class Sale_product_configuratorResource {

    @Autowired
    public ISale_product_configuratorService sale_product_configuratorService;

    @Autowired
    @Lazy
    public Sale_product_configuratorMapping sale_product_configuratorMapping;

    @PreAuthorize("hasPermission(this.sale_product_configuratorMapping.toDomain(#sale_product_configuratordto),'iBizBusinessCentral-Sale_product_configurator-Create')")
    @ApiOperation(value = "新建销售产品配置器", tags = {"销售产品配置器" },  notes = "新建销售产品配置器")
	@RequestMapping(method = RequestMethod.POST, value = "/sale_product_configurators")
    public ResponseEntity<Sale_product_configuratorDTO> create(@Validated @RequestBody Sale_product_configuratorDTO sale_product_configuratordto) {
        Sale_product_configurator domain = sale_product_configuratorMapping.toDomain(sale_product_configuratordto);
		sale_product_configuratorService.create(domain);
        Sale_product_configuratorDTO dto = sale_product_configuratorMapping.toDto(domain);
		return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorMapping.toDomain(#sale_product_configuratordtos),'iBizBusinessCentral-Sale_product_configurator-Create')")
    @ApiOperation(value = "批量新建销售产品配置器", tags = {"销售产品配置器" },  notes = "批量新建销售产品配置器")
	@RequestMapping(method = RequestMethod.POST, value = "/sale_product_configurators/batch")
    public ResponseEntity<Boolean> createBatch(@RequestBody List<Sale_product_configuratorDTO> sale_product_configuratordtos) {
        sale_product_configuratorService.createBatch(sale_product_configuratorMapping.toDomain(sale_product_configuratordtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @VersionCheck(entity = "sale_product_configurator" , versionfield = "writeDate")
    @PreAuthorize("hasPermission(this.sale_product_configuratorService.get(#sale_product_configurator_id),'iBizBusinessCentral-Sale_product_configurator-Update')")
    @ApiOperation(value = "更新销售产品配置器", tags = {"销售产品配置器" },  notes = "更新销售产品配置器")
	@RequestMapping(method = RequestMethod.PUT, value = "/sale_product_configurators/{sale_product_configurator_id}")
    public ResponseEntity<Sale_product_configuratorDTO> update(@PathVariable("sale_product_configurator_id") Long sale_product_configurator_id, @RequestBody Sale_product_configuratorDTO sale_product_configuratordto) {
		Sale_product_configurator domain  = sale_product_configuratorMapping.toDomain(sale_product_configuratordto);
        domain .setId(sale_product_configurator_id);
		sale_product_configuratorService.update(domain );
		Sale_product_configuratorDTO dto = sale_product_configuratorMapping.toDto(domain );
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorService.getSaleProductConfiguratorByEntities(this.sale_product_configuratorMapping.toDomain(#sale_product_configuratordtos)),'iBizBusinessCentral-Sale_product_configurator-Update')")
    @ApiOperation(value = "批量更新销售产品配置器", tags = {"销售产品配置器" },  notes = "批量更新销售产品配置器")
	@RequestMapping(method = RequestMethod.PUT, value = "/sale_product_configurators/batch")
    public ResponseEntity<Boolean> updateBatch(@RequestBody List<Sale_product_configuratorDTO> sale_product_configuratordtos) {
        sale_product_configuratorService.updateBatch(sale_product_configuratorMapping.toDomain(sale_product_configuratordtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorService.get(#sale_product_configurator_id),'iBizBusinessCentral-Sale_product_configurator-Remove')")
    @ApiOperation(value = "删除销售产品配置器", tags = {"销售产品配置器" },  notes = "删除销售产品配置器")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sale_product_configurators/{sale_product_configurator_id}")
    public ResponseEntity<Boolean> remove(@PathVariable("sale_product_configurator_id") Long sale_product_configurator_id) {
         return ResponseEntity.status(HttpStatus.OK).body(sale_product_configuratorService.remove(sale_product_configurator_id));
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorService.getSaleProductConfiguratorByIds(#ids),'iBizBusinessCentral-Sale_product_configurator-Remove')")
    @ApiOperation(value = "批量删除销售产品配置器", tags = {"销售产品配置器" },  notes = "批量删除销售产品配置器")
	@RequestMapping(method = RequestMethod.DELETE, value = "/sale_product_configurators/batch")
    public ResponseEntity<Boolean> removeBatch(@RequestBody List<Long> ids) {
        sale_product_configuratorService.removeBatch(ids);
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PostAuthorize("hasPermission(this.sale_product_configuratorMapping.toDomain(returnObject.body),'iBizBusinessCentral-Sale_product_configurator-Get')")
    @ApiOperation(value = "获取销售产品配置器", tags = {"销售产品配置器" },  notes = "获取销售产品配置器")
	@RequestMapping(method = RequestMethod.GET, value = "/sale_product_configurators/{sale_product_configurator_id}")
    public ResponseEntity<Sale_product_configuratorDTO> get(@PathVariable("sale_product_configurator_id") Long sale_product_configurator_id) {
        Sale_product_configurator domain = sale_product_configuratorService.get(sale_product_configurator_id);
        Sale_product_configuratorDTO dto = sale_product_configuratorMapping.toDto(domain);
        return ResponseEntity.status(HttpStatus.OK).body(dto);
    }

    @ApiOperation(value = "获取销售产品配置器草稿", tags = {"销售产品配置器" },  notes = "获取销售产品配置器草稿")
	@RequestMapping(method = RequestMethod.GET, value = "/sale_product_configurators/getdraft")
    public ResponseEntity<Sale_product_configuratorDTO> getDraft() {
        return ResponseEntity.status(HttpStatus.OK).body(sale_product_configuratorMapping.toDto(sale_product_configuratorService.getDraft(new Sale_product_configurator())));
    }

    @ApiOperation(value = "检查销售产品配置器", tags = {"销售产品配置器" },  notes = "检查销售产品配置器")
	@RequestMapping(method = RequestMethod.POST, value = "/sale_product_configurators/checkkey")
    public ResponseEntity<Boolean> checkKey(@RequestBody Sale_product_configuratorDTO sale_product_configuratordto) {
        return  ResponseEntity.status(HttpStatus.OK).body(sale_product_configuratorService.checkKey(sale_product_configuratorMapping.toDomain(sale_product_configuratordto)));
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorMapping.toDomain(#sale_product_configuratordto),'iBizBusinessCentral-Sale_product_configurator-Save')")
    @ApiOperation(value = "保存销售产品配置器", tags = {"销售产品配置器" },  notes = "保存销售产品配置器")
	@RequestMapping(method = RequestMethod.POST, value = "/sale_product_configurators/save")
    public ResponseEntity<Boolean> save(@RequestBody Sale_product_configuratorDTO sale_product_configuratordto) {
        return ResponseEntity.status(HttpStatus.OK).body(sale_product_configuratorService.save(sale_product_configuratorMapping.toDomain(sale_product_configuratordto)));
    }

    @PreAuthorize("hasPermission(this.sale_product_configuratorMapping.toDomain(#sale_product_configuratordtos),'iBizBusinessCentral-Sale_product_configurator-Save')")
    @ApiOperation(value = "批量保存销售产品配置器", tags = {"销售产品配置器" },  notes = "批量保存销售产品配置器")
	@RequestMapping(method = RequestMethod.POST, value = "/sale_product_configurators/savebatch")
    public ResponseEntity<Boolean> saveBatch(@RequestBody List<Sale_product_configuratorDTO> sale_product_configuratordtos) {
        sale_product_configuratorService.saveBatch(sale_product_configuratorMapping.toDomain(sale_product_configuratordtos));
        return  ResponseEntity.status(HttpStatus.OK).body(true);
    }

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizBusinessCentral-Sale_product_configurator-searchDefault-all') and hasPermission(#context,'iBizBusinessCentral-Sale_product_configurator-Get')")
	@ApiOperation(value = "获取数据集", tags = {"销售产品配置器" } ,notes = "获取数据集")
    @RequestMapping(method= RequestMethod.GET , value="/sale_product_configurators/fetchdefault")
	public ResponseEntity<List<Sale_product_configuratorDTO>> fetchDefault(Sale_product_configuratorSearchContext context) {
        Page<Sale_product_configurator> domains = sale_product_configuratorService.searchDefault(context) ;
        List<Sale_product_configuratorDTO> list = sale_product_configuratorMapping.toDto(domains.getContent());
        return ResponseEntity.status(HttpStatus.OK)
                .header("x-page", String.valueOf(context.getPageable().getPageNumber()))
                .header("x-per-page", String.valueOf(context.getPageable().getPageSize()))
                .header("x-total", String.valueOf(domains.getTotalElements()))
                .body(list);
	}

    @PreAuthorize("hasAnyAuthority('ROLE_SUPERADMIN','iBizBusinessCentral-Sale_product_configurator-searchDefault-all') and hasPermission(#context,'iBizBusinessCentral-Sale_product_configurator-Get')")
	@ApiOperation(value = "查询数据集", tags = {"销售产品配置器" } ,notes = "查询数据集")
    @RequestMapping(method= RequestMethod.POST , value="/sale_product_configurators/searchdefault")
	public ResponseEntity<Page<Sale_product_configuratorDTO>> searchDefault(@RequestBody Sale_product_configuratorSearchContext context) {
        Page<Sale_product_configurator> domains = sale_product_configuratorService.searchDefault(context) ;
	    return ResponseEntity.status(HttpStatus.OK)
                .body(new PageImpl(sale_product_configuratorMapping.toDto(domains.getContent()), context.getPageable(), domains.getTotalElements()));
	}


}

